//
//  MainViewController.m
//  结构
//
//  Created by 看着太阳吃烧饼 on 15/7/17.
//  Copyright (c) 2015年 看着太阳吃烧饼. All rights reserved.
//

#import "MainViewController.h"
#import "MovieViewController.h"
#import "MoreViewController.h"
#import "TopViewController.h"
#import "NewsViewController.h"
#import "CinemaViewController.h"
#import "HWbutton.h"
#import "BaseViewController.h"
#import "BaseNavViewController.h"
@interface MainViewController ()
{
    UIImageView *_selectedImageView;
}
@end

@implementation MainViewController

- (void) _createSubView {
    //第三层
    MovieViewController *vc0 = [[MovieViewController alloc] init];
    NewsViewController *vc1 = [[NewsViewController alloc] init];
    TopViewController *vc2 = [[TopViewController alloc] init];
    CinemaViewController *vc3 = [[CinemaViewController alloc] init];
    MoreViewController *vc4 = [[MoreViewController alloc] init];
    NSArray *vcArray = [[NSArray alloc] init];
    vcArray = @[vc0,vc1,vc2,vc3,vc4];
    
    //第二层
    NSMutableArray *navArray = [[NSMutableArray alloc] init];
    for (int i = 0; i<5; i++) {
        BaseNavViewController *nav = [[BaseNavViewController alloc] initWithRootViewController:vcArray[i]];
        [navArray addObject:nav];
        self.viewControllers = navArray;
    }
    
}

//移除tabBar子视图
- (void)_setTabBar {
    for (UIView *subView in self.tabBar.subviews) {
        Class cls = NSClassFromString(@"UITabBarButton");
        if ([subView isKindOfClass:cls]) {
            [subView removeFromSuperview];
        }
    }
    self.tabBar.translucent = YES;
    NSArray *titles = @[@"电影",@"新闻",@"排行榜",@"观影",@"更多"];
    NSArray *imageName = @[@"movie_home.png",@"msg_new.png",@"yellow@2x.png",@"icon_cinema.png",@"more_select_setting.png"];
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_bg_all"]];
    
    CGFloat height = CGRectGetHeight(self.tabBar.frame);
    CGFloat width = CGRectGetWidth(self.tabBar.frame)/5;
    
//    for (int i = 0; i<5; i++) {
//        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i*width, 0, width, height)];
//        [button setTitle:titles[i] forState:UIControlStateNormal];
//        [button setImage:[UIImage imageNamed:imageName[i]] forState:UIControlStateNormal];
//        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        [self.tabBar addSubview:button];
//        button.titleLabel.font = [UIFont systemFontOfSize:11];
//        button.titleEdgeInsets = UIEdgeInsetsMake(30, -18, 0, 0);
//        button.imageEdgeInsets = UIEdgeInsetsMake(-10, 20, 0, 0);
//    }
    _selectedImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"selectTabbar_bg_all@2x"]];
    _selectedImageView.frame = CGRectMake(0, 0, self.tabBar.frame.size.width/5, self.tabBar.frame.size.height);
    
    [self.tabBar addSubview:_selectedImageView];
    
    for (int i = 0; i<5; i++) {
        HWbutton *button = [[HWbutton alloc] initWithFrame:CGRectMake(i*width, 0, width, height) withImageName:imageName[i] withTitle:titles[i]];
        button.tag = i;
        [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBar addSubview:button];
    }
    
}

- (void)btnAction:(HWbutton *)button {
    NSInteger index = button.tag;
    self.selectedIndex = index;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    _selectedImageView.center = button.center;
    [UIView commitAnimations];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createSubView];
    [self _setTabBar];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
